package jmu.db.test.xtest.common ;


public class IdentifiedContainer < T > extends Container < T >
{
    private Identity identity = null ;

    public IdentifiedContainer ( final Identity id )
    {
        setIdentity ( id ) ;
    }

    @Override
    public boolean equals ( final Object o )
    {
        if ( ! (o instanceof Identity) )
        {
            return false ;
        }

        // compare by identities
        return identity.equals ( ((IdentifiedContainer < ? >) o).getIdentity () ) ;
    }

    public Identity getIdentity ()
    {
        return identity ;
    }

    @Override
    public int hashCode ()
    {
        if ( null != identity )
        {
            return identity.hashCode () ;
        }

        return super.hashCode () ;
    }

    private void setIdentity ( final Identity identity )
    {
        if ( null == identity )
        {
            throw new IllegalArgumentException ( "identity can't be null" ) ;
        }

        this.identity = identity ;
    }
}
